package com.gpc.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MyController {

    @RequestMapping("/toLogin")
    public String toLogin(){
        return "login";
    }

    @RequestMapping("/login")
    public String login(String username, String password, Model model){
        //获取当前用户
        Subject subject = SecurityUtils.getSubject();
        //封装用户登录数据
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);

        try {
            subject.login(token);//执行登录方法
            return "index";
        } catch (UnknownAccountException e) {
            model.addAttribute("msg","用户名出错");
            return "login";
        }catch (IncorrectCredentialsException e){
            model.addAttribute("msg","密码出错");
            return "login";
        }
    }

    @RequestMapping({"/", "/index"})
    public String goIndex(){
        return "index";
    }

    @RequestMapping("/main/contact")
    public String contact(){
        return "main/contact";
    }

    @RequestMapping("/main/menu")
    public String menu(){
        return "main/menu";
    }

    @RequestMapping("/main/regular")
    public String regular(){
        return "main/regular-page";
    }

    @RequestMapping("/noauth")
    @ResponseBody
    public String noauth(){
        return "noauth";
    }

    @RequestMapping("/logout")
    @ResponseBody
    public String logout(){
        return "logout";
    }

}
